/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../emulator.h"
#include "../gpsfix.h"

#include <stdlib.h>
#include <QtCore/QTime>

#define RAND(min, max) (min + rand()/(float)RAND_MAX * (max-min))

using namespace greenhills::gps;

CEmulator::CEmulator(QObject *parent)
    :QObject(parent)
{
    mTimerId = 0;
    mStep = 5;
}

CEmulator::CEmulator(const CGPSPosition &pPosition, int pDistance, int pTimer,
                     int pStep, QObject *parent):QObject(parent)
{
  mPosition = pPosition;
  mDistance = pDistance;
  mTimer = pTimer;
  mStep = pStep;
  mTimerId = 0;
}



void CEmulator::start()
{
    if (mTimerId != 0)
        killTimer(mTimerId);

    mTimerId = startTimer(5000);
}

void CEmulator::stop()
{
    Q_EMIT( GPGGAreceived(QTime(), CGPSPosition(), CGPSFix(),0,0,0,0,0,0) );
    Q_EMIT( GPVTGreceived( 0, 0, 0, 0) );

    if (mTimerId != 0)
        killTimer(mTimerId);

    mTimerId = 0;
}

void CEmulator::timerEvent(QTimerEvent *pEvent)
{
    Q_UNUSED(pEvent);

    QTime time = QTime::currentTime();
    CGPSFix fix = CGPSFix::DGPSfix;

    //RANDOM position, altitude, speed, direction etc...
//    CGPSPosition position = gps::CGPSPosition(
//        gps::CGPSCoordinate((long)(RAND(48, 49)*
//            CGPSCoordinate::COORD_1_DEGREE)),
//        gps::CGPSCoordinate((long)(RAND(18, 19)*
//            CGPSCoordinate::COORD_1_DEGREE)));

    if(mStep >= 360)mStep = 0;
    int satelites = (int)RAND(5, 10);
    float hdop = RAND(1,3);
    float altitude = RAND(230, 280);
    float geoidHeight = RAND(230, 280);
    float dgpsUpdateDelay = RAND(0, 1000);
    int dgpsStationId = (int)RAND(0, 10);

    float headingTrueNorth = 0;
        if(mStep>=270) headingTrueNorth = mStep-270;
        else headingTrueNorth = mStep+90;
    float headingMagnetic = RAND(10, 70);
    float speedInKnots = RAND(0, 60);
    float speedInKilometers = RAND(0, 20);


   CGPSPosition center(
           CGPSCoordinate(17*CGPSCoordinate::COORD_1_DEGREE),
           CGPSCoordinate(50*CGPSCoordinate::COORD_1_DEGREE));
   CGPSPosition position = center.positionTo(mStep, mDistance);

  mStep +=20;
    Q_EMIT( GPGGAreceived(time, position, fix, satelites, hdop, altitude,
        geoidHeight, dgpsUpdateDelay, dgpsStationId) );
    Q_EMIT( GPVTGreceived( headingTrueNorth, headingMagnetic,
        speedInKnots, speedInKilometers) );
}
